package jdbc;

import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;

/**
 * @classname:JdbcCon
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/27 21:06
 * @version: 0.1
 * @since:1.8
 */
public class DBHelpers {
    private DBHelpers(){}


    private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal(){
        @SneakyThrows
        @Override
        protected Object initialValue() {
            return DriverManager.getConnection(PropUtil.getValue("jdbc.url"),PropUtil.getValue("jdbc.username"),PropUtil.getValue("jdbc.password"));
        }
    };

    public static Connection getJdbcCon(){
        return THREAD_LOCAL.get();
    }
    public static void CloseResources(Connection connection){
        Objects.requireNonNull(connection);
        try {
            connection.close();
            THREAD_LOCAL.remove();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
